Skip to content

Conversation

bilhackmac
Copy link

@bilhackmac bilhackmac commented Jan 12, 2023

INF / -INF / NAN are not always rendered as desired when transformed to string using number_format and printf.

echo number_format(INF), ' @ ', number_format(-INF), ' @ ', number_format(NAN);
# inf @ inf @ nan
// => Missing minus sign on second inf


printf('%f @ %f @ %f', INF, -INF, NAN);
# INF @ INF @ NaN
// => Missing minus sign on second INF
// => NaN is inconsistant with other 'not a number' rendering (nan or NAN)

printf('%+f', INF);
# INF
// => Missing plus sign before INF

printf('%+0f', INF);
printf('%+0f', -INF);
printf('%+0f', NAN);
# +NF
# -NF
# +aN
// => Buggy

printf('@%10f@', INF);
printf('@%-10f@', INF);
# @INF@
# @INF@
// => No right or left padding

This PR fix these cases

@bilhackmac bilhackmac changed the title Fix INF/NAN to string rendering WIP: Fix INF/NAN to string rendering Jan 12, 2023
@bilhackmac bilhackmac changed the title WIP: Fix INF/NAN to string rendering Fix INF/NAN to string rendering Jan 12, 2023
@Girgias
Copy link
Member

Girgias commented Jan 13, 2023

Somewhat related to #9209/#10201

@bilhackmac
Copy link
Author

No I see these issue and PR before but it's not related

@Girgias
Copy link
Member

Girgias commented Jan 13, 2023

No I see these issue and PR before but it's not related

I said "somewhat" related. I know full well that this is not the same issue as in that issue/PR, but it's a similar issue enough that it probably indicates there is a larger problem at stake here in with how we handle these cases across the codebase.

@bilhackmac
Copy link
Author

Any progress on this problem ?

@marc-mabe
Copy link
Contributor

For the case of number_format wouldn't it make more sense to match what NumberFormatter is returning for [-]INF ?

$ php -r 'var_dump((new NumberFormatter("en_US", NumberFormatter::DECIMAL))->format(INF));'
string(3) "∞"

$ php -r 'var_dump((new NumberFormatter("en_US", NumberFormatter::DECIMAL))->format(-INF));'
string(4) "-∞"

$ php -r 'var_dump((new NumberFormatter("en_US", NumberFormatter::DECIMAL))->format(NAN));'
string(3) "NaN"

@bilhackmac
Copy link
Author

It make sense for me but this small change can have large impact and should be discussed by core PHP team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants